package com.fuxian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.fuxian.dao.UserDao;
import com.fuxian.model.User;
import com.fuxian.service.UserService;
import com.fuxian.util.DateUtile;

@Repository
public class UserServiceImpl implements UserService {

	@Resource
	private UserDao userDao;

	public void addUser(User user) {
		user.setCreate_user(user.getUser_name());
		user.setCreate_time(DateUtile.getTimestamp());
		userDao.addUser(user);
	}

	public void deleteUserById(int id) {
		userDao.deleteUserById(id);

	}

	public void modifyUser(User user) {
		user.setModify_time(DateUtile.getTimestamp());
		userDao.modifyUser(user);
	}

	public User selectUserById(int id) {
		return userDao.selectUserById(id);
	}

	public User selectUserByUserName(String userName) {

		return userDao.selectUserByUserName(userName);
	}

	public User selectUserByUserNamePwd(User user) {

		return userDao.selectUserByUserNamePwd(user);
	}

	public List<User> selectUsersByStatus(String status,int start,int size) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rstatus", status);
		map.put("start", start);
		map.put("size", size);
		return userDao.selectUsersByStatus(map);
	}

	public List<User> selectUsersByuType(String uType,int start,int size) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("utype", uType);
		map.put("start", start);
		map.put("size", size);
		return userDao.selectUsersByUtype(map);
	}

	public List<User> selectAllUsers(int start,int size) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", start);
		map.put("size", size);
		return userDao.selectAllUsers(map);
	}

	public boolean login(String userName, String pwd) {
		User user = new User();
		user.setUser_name(userName);
		user.setPassword(pwd);
		User returnUser = userDao.selectUserByUserNamePwd(user);
		if (returnUser != null && !returnUser.equals(null)) {
			return true;
		}
		return false;
	}

	public boolean isExistUser(String userName) {
		User user = userDao.selectUserByUserName(userName);
		if (user != null && !user.equals(null)) {
			return true;
		}
		return false;
	}

	public boolean register(String userName, String pwd, String phoneNo,
			String qqNo) {
		User user = new User();
		user.setUser_name(userName);
		user.setPassword(pwd);
		user.setPhone_no(phoneNo);
		user.setQq_num(qqNo);
		int i = userDao.addUser(user);
		if (i == 1) {
			return true;
		} else {
			return false;
		}

	}

	public boolean changPwd(String userName, String pwd) {
		User user = new User();
		user.setUser_name(userName);
		user.setPassword(pwd);
		int i = userDao.modifyUser(user);
		if (i == 1)
			return true;
		else
			return false;
	}

	public int selectCount() {
		
		return userDao.selectCount();
	}

}
